<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>寻找两个正序数组的中位数 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-dots {
            display: flex;
            gap: 8px;
        }
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            color: #d4d4d4;
        }
        .keyword { color: #c586c0; }
        .function { color: #dcdcaa; }
        .number { color: #b5cea8; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .operator { color: #d4d4d4; }
        .builtin { color: #4ec9b0; }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="gradient-bg text-white py-20 px-6">
        <div class="max-w-6xl mx-auto animate-fadeInUp">
            <div class="text-center">
                <div class="inline-flex items-center bg-white bg-opacity-20 rounded-full px-4 py-2 mb-6">
                    <i class="fas fa-code mr-2"></i>
                    <span class="text-sm font-medium">高级算法解析</span>
                </div>
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                    寻找两个正序数组的中位数
                </h1>
                <p class="text-xl md:text-2xl text-gray-200 max-w-3xl mx-auto leading-relaxed">
                    探索如何在 O(log(m+n)) 时间复杂度内，优雅地解决这道经典的算法难题
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <div class="text-3xl font-bold">O(log n)</div>
                        <div class="text-sm text-gray-300">时间复杂度</div>
                    </div>
                    <div class="text-center">
                        <div class="text-3xl font-bold">二分查找</div>
                        <div class="text-sm text-gray-300">核心算法</div>
                    </div>
                    <div class="text-center">
                        <div class="text-3xl font-bold">Hard</div>
                        <div class="text-sm text-gray-300">难度等级</div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-16">
        <!-- Problem Description -->
        <section class="mb-16 animate-fadeInUp">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center mr-4">
                        <i class="fas fa-question-circle text-purple-600 text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
                </div>
                <p class="text-lg text-gray-700 leading-relaxed">
                    <span class="drop-cap serif-font">给</span>定两个大小分别为 m 和 n 的正序（从小到大）数组 nums1 和 nums2。请你找出并返回这两个正序数组的中位数。算法的时间复杂度应为 O(log (m+n))。
                </p>
                <div class="mt-6 grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div class="bg-gray-50 rounded-lg p-4">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-list-ol text-purple-600 mr-2"></i>
                            <span class="font-semibold text-gray-700">输入条件</span>
                        </div>
                        <p class="text-sm text-gray-600">两个已排序的数组</p>
                    </div>
                    <div class="bg-gray-50 rounded-lg p-4">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-chart-line text-purple-600 mr-2"></i>
                            <span class="font-semibold text-gray-700">输出要求</span>
                        </div>
                        <p class="text-sm text-gray-600">两数组合并后的中位数</p>
                    </div>
                    <div class="bg-gray-50 rounded-lg p-4">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-clock text-purple-600 mr-2"></i>
                            <span class="font-semibold text-gray-700">时间限制</span>
                        </div>
                        <p class="text-sm text-gray-600">O(log(m+n))</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Algorithm Visualization -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.2s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center mr-4">
                        <i class="fas fa-project-diagram text-blue-600 text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">算法思路可视化</h2>
                </div>
                <div class="mermaid">
                    graph TD
                        A[开始] --> B{比较数组长度}
                        B -->|nums1 > nums2| C[交换数组]
                        B -->|nums1 <= nums2| D[初始化二分查找边界]
                        C --> D
                        D --> E[计算分割点 partitionX]
                        E --> F[计算对应分割点 partitionY]
                        F --> G[获取左右边界值]
                        G --> H{检查分割是否有效}
                        H -->|有效| I{总长度奇偶性}
                        I -->|偶数| J[返回左最大和右最小的平均值]
                        I -->|奇数| K[返回左侧最大值]
                        H -->|左侧过大| L[调整右边界]
                        H -->|右侧过大| M[调整左边界]
                        L --> E
                        M --> E
                        J --> N[结束]
                        K --> N
                        
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style N fill:#9f9,stroke:#333,stroke-width:2px
                        style H fill:#ff9,stroke:#333,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.4s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center mr-4">
                        <i class="fas fa-lightbulb text-green-600 text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">核心思想解析</h2>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div class="bg-gradient-to-br from-purple-50 to-blue-50 rounded-xl p-6">
                        <h3 class="text-xl font-bold text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-search text-purple-600 mr-2"></i>
                            二分查找的妙用
                        </h3>
                        <p class="text-gray-700 leading-relaxed">
                            通过在较短的数组上进行二分查找，找到一个分割点，使得两个数组被分割后的左半部分元素总数等于右半部分（或相差1）。
                        </p>
                    </div>
                    <div class="bg-gradient-to-br from-blue-50 to-green-50 rounded-xl p-6">
                        <h3 class="text-xl font-bold text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-balance-scale text-blue-600 mr-2"></i>
                            平衡分割的艺术
                        </h3>
                        <p class="text-gray-700 leading-relaxed">
                            确保左半部分的最大值小于等于右半部分的最小值，这样中位数就在分割点的边界值中产生。
                        </p>
                    </div>
                </div>
                <div class="mt-6 bg-gray-50 rounded-xl p-6">
                    <h3 class="text-lg font-bold text-gray-800 mb-3">关键洞察</h3>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">中位数将数组分成两个等长的部分</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">左侧所有元素 ≤ 右侧所有元素</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">只需在较短数组上二分，降低复杂度</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16 animate-fadeInUp" style="animation-delay: 0.6s;">
            <div class="bg-white rounded-2xl shadow-lg overflow-hidden card-hover">
                <div class="flex items-center p-8 pb-0">
                    <div class="w-12 h-12 bg-orange-100 rounded-lg flex items-center justify-center mr-4">
                        <i class="fas fa-code text-orange-600 text-xl"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">代码实现</h2>
                </div>
                <div class="p-8 pt-6">
                    <div class="code-block">
                        <div class="code-header">
                            <div class="code-dots">
                                <div class="dot dot-red"></div>
                                <div class="dot dot-yellow"></div>
                                <div class="dot dot-green"></div>
                            </div